﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonFramework.Utility;
using Back.Xinkui.DataAccess.Company;
using Back.Xinkui.WebModel;
using Back.Xinkui.Entity.Company;
using Back.Xinkui.Common.Enum;

public partial class company_productDetail : PageAspxBase
{
    protected string imgProduct
    { get; set; }

    protected int ProductId
    {
        get
        {
            return WebHelper.GetQueryStringValue<int>("pid", 0);
        }
    }

    protected int AuditStatus { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindProduct();
        }
    }

    private void BindProduct()
    {
        var pro = CompanyDataAccess.GetProductByProductId(ProductId);
        if (pro != null)
        {
            this.txtApplication.Text = pro.Application;
            this.txtCertificateg.Text = pro.CertificateIDS;
            this.txtDescription.Text = pro.ProductDescription;
            this.txtKeyWord.Text = pro.Keywords;
            this.txtMainKeyWord.Text = pro.MainKeyword;
            this.txtOem.Text = pro.OEMNO;
            this.txtPlaceOfOrigin.Text = pro.PlaceOfOrigin;
            this.hidLogo.Value = pro.PicImgs;
            if (!string.IsNullOrEmpty(pro.viewImage))
            {
                this.viewImg.ImageUrl = pro.viewImage;
            }
            if (pro.KeyProducts == 1)
            {
                this.radioYes.Checked = true;
            }
            if (pro.KeyProducts == 0)
            {
                this.radioNo.Checked = true;
            }

            this.txtProdcutName.Text = pro.ProductName;

            UC_Category1.CategoryID = pro.CategoryID;
            AuditStatus = (int)pro.AuditStatus;
        }
    }
    /// <summary>
    /// 通过
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnApprove_Click(object sender, EventArgs e)
    {
        CompanyDataAccess.UpdateProductStatus(ProductId, (int)EAuditStatus.Approve);
        ShowAndReloadOpner("审核通过");
    }
    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //WebHelper.GetFormHtmlValue<int>("hidCaegory", 0);
        //获取表单数据
        ProdcutEntity prodcut = GetPostProdcutInfo();

        if (ProductId > 0)
        {
            prodcut.ProductID = ProductId;
            //更新产品
            prodcut.UpdateIP = WebHelper.UserIP;
            CompanyDataAccess.SaveProductInfo(prodcut);
            ShowAndReloadOpner("保存成功");
        }
        else
        {
            //新增产品
            prodcut.CreateIP = WebHelper.UserIP;
            prodcut.CompanyID = WebHelper.GetQueryStringValue<int>("companyId", 0);
            if (prodcut.CompanyID > 0)
            {
                CompanyDataAccess.CreateProductInfo(prodcut);
                ShowAndReloadOpner("添加成功");
            }
            else
            {
                ShowAndReloadOpner("添加失败");
            }
        }
    }
    /// <summary>
    /// 拒绝
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReject_Click(object sender, EventArgs e)
    {
        CompanyDataAccess.UpdateProductStatus(ProductId, (int)EAuditStatus.Reject);
        ShowAndReloadOpner("操作成功");
    }
    /// <summary>
    /// 获取产品信息
    /// </summary>
    /// <returns></returns>
    private ProdcutEntity GetPostProdcutInfo()
    {
        ProdcutEntity entity = new ProdcutEntity();
        entity.ProductName = this.txtProdcutName.Text.Trim();
        entity.CategoryID = WebHelper.GetFormHtmlValue<int>("hidCaegory", 0);
        entity.MainKeyword = this.txtMainKeyWord.Text.Trim();
        entity.Keywords = this.txtKeyWord.Text.Trim();
        entity.PlaceOfOrigin = this.txtPlaceOfOrigin.Text.Trim();
        entity.CertificateIDS = this.txtCertificateg.Text.Trim();
        entity.Application = this.txtApplication.Text.Trim();
        entity.OEMNO = this.txtOem.Text.Trim();
        entity.ProductDescription = this.txtDescription.Text.Trim();
        if (this.radioYes.Checked)
        {
            entity.KeyProducts = 1;
        }
        if (this.radioNo.Checked)
        {
            entity.KeyProducts = 0;
        }
        entity.PicImgs = this.hidLogo.Value;
        return entity;
    }
}